package com.tiamo.traders.servicefront.ServiceFrontImpl;

import com.tiamo.traders.entity.Business;
import com.tiamo.traders.entity.Nav;
import com.tiamo.traders.entity.News;
import com.tiamo.traders.entity.Partners;
import com.tiamo.traders.mapperfront.HomeMapper;
import com.tiamo.traders.servicefront.HomeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.concurrent.TimeUnit;

@Service
public class HomeServiceImpl implements HomeService {
    @Autowired
    HomeMapper homeMapper;

    @Autowired
    RedisTemplate redisTemplate;

    public Map home() {
        Map map = new HashMap();
        List<Nav> navList = new ArrayList<>();
        List<Business> businessList;
        List<News> newsList;
        List<Partners> partnersList = new ArrayList<>();
        /*
         * 主页的缓存
         * 1.标题缓存
         * hash
         * */
        boolean isHave = redisTemplate.hasKey("nav");
        if (isHave) {
            for (int i = 0; i < 6; i++) {
                String res = (String) redisTemplate.boundHashOps("nav").get("nav" + i);
                String title = (String) redisTemplate.boundHashOps("title").get("title" + i);
                System.out.println(res);
                Nav nav = new Nav();
                nav.setHref(res);
                nav.setNav(title);
                navList.add(nav);
                //navList.add((Nav) redisTemplate.boundHashOps("nav").get("nav"+i));
            }
        } else {
            navList = homeMapper.navFindAll();
            for (int i = 0; i < navList.size(); i++) {
                redisTemplate.boundHashOps("nav").put("nav" + i, navList.get(i).getHref());
                redisTemplate.boundHashOps("title").put("title" + i, navList.get(i).getNav());
                redisTemplate.boundValueOps("nav").expire(24, TimeUnit.MINUTES);
            }
        }

        /*
         * 前端业务缓存，直接存的list
         * */
        boolean isEmpty = redisTemplate.hasKey("businessList");
        if (isEmpty) {
            List res = redisTemplate.boundListOps("businessList").range(0, 6);
            businessList = (List<Business>) res.get(0);
            System.out.println(businessList);
        } else {
            businessList = homeMapper.businessFindAll();
            redisTemplate.boundListOps("businessList").rightPushAll(businessList);
        }

        /*
         * 前端新闻缓存
         *直接存list
         * */
        boolean isNews = redisTemplate.hasKey("newsList");
        if (isNews) {

            List res = redisTemplate.boundListOps("newsList").range(0, 3);
            newsList = (List<News>) res.get(0);
        } else {
            newsList = homeMapper.newsFindAll();
            redisTemplate.boundListOps("newsList").rightPushAll(newsList);
        }
        /*
         * 前端合作伙伴图片
         * set
         * 因为不需要顺序
         * */
        boolean isPartners = redisTemplate.hasKey("imgHref");
        if (isPartners) {
            Set res = redisTemplate.boundSetOps("imgHref").members();
            Iterator<String> iterator = res.iterator();
            while (iterator.hasNext()) {
                Partners partners = new Partners();
                partners.setImg(iterator.next());
                partnersList.add(partners);
            }
        } else {
            partnersList = homeMapper.partnersFindAll();
//            for (int i = 0; i < 10; i++) {
//                redisTemplate.boundSetOps("imgHref").add(partnersList.get(i).getImg());
//            }
            for (Partners partners : partnersList) {
                redisTemplate.boundSetOps("imgHref").add(partners.getImg());
            }
        }
        map.put("navList", navList);
        map.put("newsList", newsList);
        map.put("businessList", businessList);
        map.put("partnersList", partnersList);
        return map;
    }

    @Override
    public List<Nav> navFindAll() {
        return homeMapper.navFindAll();
    }

    @Override
    public List<Partners> partnersFindAll() {
        return homeMapper.partnersFindAll();
    }

    @Override
    public List<News> newsFindAll() {
        return homeMapper.newsFindAll();
    }

    @Override
    public List<Business> businessFindAll() {
        return homeMapper.businessFindAll();
    }
}
